package com.jsmile.cloud.tradecenter.cart.factory;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.jsmile.cloud.goodscenter.api.feign.SkuServiceApi;
import com.jsmile.cloud.goodscenter.api.req.spu.ReqSkuCheck;
import com.jsmile.cloud.goodscenter.api.res.ResSku;
import com.jsmile.cloud.tradecenter.api.exception.TradeException;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqAddCart;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqCartDetial;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqDeleteCart;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqUpdateCart;
import com.jsmile.cloud.tradecenter.api.res.ResCart;
import com.jsmile.cloud.tradecenter.api.res.ResCartDetail;
import com.jsmile.cloud.tradecenter.cart.CartApplicationService;
import com.jsmile.cloud.tradecenter.cart.model.ShoppingCart;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.cache.kit.SpringKit;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
@AllArgsConstructor
public class CartFactory {

    private final CartApplicationService cartApplicationService;

    public ResCartDetail cartDetial(ReqCartDetial reqCartDetial) {
        ResCartDetail resCartDetail = new ResCartDetail();
        List<ResCart> cartList = Lists.newArrayList();// 有效商品
        List<ResCart> invalidCartList = Lists.newArrayList();// 无效商品
        List<ShoppingCart> shoppingCarts = cartApplicationService.cartDetial(reqCartDetial);
        if (!CollectionUtils.isEmpty(shoppingCarts)) {
            JSmileResult<List<ResSku>> jSmileResult = SpringKit.getBean(SkuServiceApi.class).skuCheck(ReqSkuCheck.builder().skuList(shoppingCarts.stream().map(x -> {
                return x.createSkuCheck();
            }).collect(Collectors.toList())).build());
            if (jSmileResult.getResSuccess()) {
                Map<Long, ResSku> skuMap = jSmileResult.getResData().stream().collect(Collectors.toMap(ResSku::getSkuId, a -> a, (k1, k2) -> k1));
                Map<Long, List<ShoppingCart>> shopMap = shoppingCarts.stream().collect(Collectors.groupingBy(ShoppingCart::getShopId));
                shopMap.keySet().forEach(key -> {
                    List<ResCart.CartDetial> cartDetials = Lists.newArrayList();
                    List<ResCart.CartDetial> invalidCartDetials = Lists.newArrayList();
                    shopMap.get(key).forEach(shoppingCart -> {
                        if (!skuMap.containsKey(shoppingCart.getSkuId()) || !skuMap.get(shoppingCart.getSkuId()).getHasGoodsSale()) {
                            invalidCartDetials.add(shoppingCart.createCartDetial(skuMap.get(shoppingCart.getSkuId())));
                        } else {
                            cartDetials.add(shoppingCart.createCartDetial(skuMap.get(shoppingCart.getSkuId())));
                        }
                    });
                    if (!CollectionUtils.isEmpty(cartDetials)) {
                        ResCart resCart = new ResCart();
                        resCart.setShopId(key);
                        resCart.setCartDetials(cartDetials);
                        cartList.add(resCart);
                    }
                    if (!CollectionUtils.isEmpty(invalidCartDetials)) {
                        ResCart invalidResCart = new ResCart();
                        invalidResCart.setShopId(key);
                        invalidResCart.setCartDetials(invalidCartDetials);
                        invalidCartList.add(invalidResCart);
                    }
                });
            } else {
                throw new TradeException(jSmileResult.getResCode(), jSmileResult.getResMsg(), null);
            }
        }
        resCartDetail.setInvalidCartList(invalidCartList);
        resCartDetail.setCartList(cartList);
        return resCartDetail;
    }

    public Boolean addCart(ReqAddCart reqAddCart) {
        return cartApplicationService.addCart(reqAddCart);
    }

    public Boolean updateCart(ReqUpdateCart reqUpdateCart) {
        return cartApplicationService.updateCart(reqUpdateCart);
    }

    public Boolean clearCart(ReqCartDetial reqCartDetial) {
        return cartApplicationService.clearCart(reqCartDetial);
    }

    public Boolean deleteCart(ReqDeleteCart reqDeleteCart) {
        return cartApplicationService.deleteCart(reqDeleteCart);
    }

    public long countCart(ReqCartDetial reqCartDetial) {
        return cartApplicationService.countCart(reqCartDetial);
    }

}
